﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class CourseService
    {
       readonly ICourseEntity entity;
       readonly IClass_FeeTimeEntity class_feeentity;
       public CourseService()
       {
           this.entity = new CourseEntity();
           this.class_feeentity = new Class_FeeTimeEntity();

       }


       #region SQL
       const string courdecode = " select top 1 Code from Course order by ID DESC";
       #endregion
       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if (Validate((Course)_model, ref mess))
               {
                   var obj = (CoreData.Course)_model;
                   var id = obj.ID;
                   if (id == 0)
                   {
                       var course_id = entity.Save(obj, Table.Course.ToString());
                       /*
                       //dong 1 lan
                       Course_FeeTime f1 = new Course_FeeTime();
                       f1.CourseID = course_id;
                       f1.Times = "Một lần";
                       f1.Type = 1;

                       // nếu là một lần thì láy ngày bắt đầu khóa học -1 tháng
                       // ví dụ khóa khai giảng thang 8 thì start là tháng 7 và end là tháng 8

                       f1.StartDate1 = obj.StartDate.AddMonths(-1);
                       f1.EndDate1 = obj.StartDate;
                       f1.Money1 = obj.Price + ValueDefine.dongmotlan;
                       course_feeentity.Save(f1, Table.Course_FeeTime.ToString());

                       //dong 2 lan
                       Course_FeeTime f2 = new Course_FeeTime();
                       f2.CourseID = course_id;
                       f2.Type = 2;
                       f2.Times = "Hai lần";
                       // nếu là 2 lần thì lan 1 láy ngày bắt đầu khóa học -3 tháng
                       //lan 2 thu ngay khai giang den bat dat cua lan 2
                       // ví dụ khóa khai giảng thang 8 thì start là tháng 7 và end là tháng 8

                       //f2.StartDate1 = obj.StartDate;
                       //f2.EndDate1 = f2.StartDate1.AddMonths(3);

                       //f2.StartDate2 = f2.EndDate1;
                       //f2.EndDate2 = obj.EndDate;

                       f2.StartDate1 = obj.StartDate.AddMonths(-3);
                       f2.EndDate1 = obj.StartDate;

                       f2.StartDate2 = f2.EndDate1;
                       f2.EndDate2 = obj.StartDate.AddMonths(3);

                       f2.Money1 = (obj.Price + ValueDefine.donghailan) / 2;
                       f2.Money2 = (obj.Price + ValueDefine.donghailan) / 2;
                       course_feeentity.Save(f2, Table.Course_FeeTime.ToString());

                       if (obj.Type != 0)//neu la khoa ngan han thi chi tao 2 lan
                       {
                           ////dong 3 lan (bỏ đi)
                           //Course_FeeTime f3 = new Course_FeeTime();
                           //f3.CourseID = course_id;
                           //f3.Type = 3;
                           //f3.Times = "Ba lần";
                           //f3.StartDate1 = obj.StartDate;
                           //f3.EndDate1 = f3.StartDate1.AddMonths(2);

                           //f3.StartDate2 = f3.EndDate1;
                           //f3.EndDate2 = f3.StartDate2.AddMonths(2);

                           //f3.StartDate3 = f3.EndDate2;
                           //f3.EndDate3 = obj.EndDate;

                           //f3.Money1 = obj.Price / 3;
                           //f3.Money2 = obj.Price / 3;
                           //f3.Money3 = obj.Price / 3;
                           //course_feeentity.Save(f3, Table.Course_FeeTime.ToString());


                           //dong 4 lan
                           Course_FeeTime f4 = new Course_FeeTime();
                           f4.CourseID = course_id;
                           f4.Type = 4;
                           f4.Times = "Bốn lần";


                           //f4.StartDate1 = obj.StartDate;
                           //f4.EndDate1 = f4.StartDate1.AddMonths(1).AddDays(15);

                           //f4.StartDate2 = f4.EndDate1;
                           //f4.EndDate2 = f4.StartDate2.AddMonths(1).AddDays(15);


                           //f4.StartDate3 = f4.EndDate2;
                           //f4.EndDate3 = f4.StartDate3.AddMonths(1).AddDays(15);

                           //f4.StartDate4 = f4.EndDate3;
                           //f4.EndDate4 = obj.EndDate;


                           f4.StartDate1 = obj.StartDate.AddMonths(-1).AddDays(-15);
                           f4.EndDate1 = obj.StartDate;

                           f4.StartDate2 = f4.EndDate1;
                           f4.EndDate2 = f4.StartDate2.AddMonths(1).AddDays(15);

                           f4.StartDate3 = f4.EndDate2;
                           f4.EndDate3 = f4.StartDate3.AddMonths(1).AddDays(15);

                           f4.StartDate4 = f4.EndDate3;
                           f4.EndDate4 = f4.StartDate4.AddMonths(1).AddDays(15);

                           f4.Money1 = (obj.Price + ValueDefine.dongbonlan) / 4;
                           f4.Money2 = (obj.Price + ValueDefine.dongbonlan) / 4;
                           f4.Money3 = (obj.Price + ValueDefine.dongbonlan) / 4;
                           f4.Money4 = (obj.Price + ValueDefine.dongbonlan) / 4;
                           course_feeentity.Save(f4, Table.Course_FeeTime.ToString());


                           //dong 6 lan
                           Course_FeeTime f6 = new Course_FeeTime();
                           f6.CourseID = course_id;
                           f6.Type = 6;
                           //f6.StartDate1 = obj.StartDate;
                           //f6.EndDate1 = f6.StartDate1.AddMonths(1);
                           f6.Times = "Sáu lần";
                           //f6.StartDate2 = f6.EndDate1;
                           //f6.EndDate2 = f6.StartDate2.AddMonths(1);

                           //f6.StartDate3 = f6.EndDate2;
                           //f6.EndDate3 = f6.StartDate3.AddMonths(1);

                           //f6.StartDate4 = f6.EndDate3;
                           //f6.EndDate4 = f6.StartDate4.AddMonths(1);

                           //f6.StartDate5 = f6.EndDate4;
                           //f6.EndDate5 = f6.StartDate5.AddMonths(1);

                           //f6.StartDate6 = f6.EndDate5;
                           //f6.EndDate6 = obj.EndDate;

                           f6.StartDate1 = obj.StartDate.AddMonths(-1);
                           f6.EndDate1 = obj.StartDate;

                           f6.StartDate2 = f6.EndDate1;
                           f6.EndDate2 = f6.StartDate2.AddMonths(1);

                           f6.StartDate3 = f6.EndDate2;
                           f6.EndDate3 = f6.StartDate3.AddMonths(1);

                           f6.StartDate4 = f6.EndDate3;
                           f6.EndDate4 = f6.StartDate4.AddMonths(1);

                           f6.StartDate5 = f6.EndDate4;
                           f6.EndDate5 = f6.StartDate5.AddMonths(1);

                           f6.StartDate6 = f6.EndDate5;
                           f6.EndDate6 = f6.StartDate6.AddMonths(1);

                           f6.Money1 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           f6.Money2 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           f6.Money3 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           f6.Money4 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           f6.Money5 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           f6.Money6 = (obj.Price + ValueDefine.dongsaulan) / 6;
                           course_feeentity.Save(f6, Table.Course_FeeTime.ToString());
                       }
                       */




                       return course_id;

                   }
                   else
                   {
                       return entity.Update(obj, Table.Course.ToString());
                   }
               }
               else { return -1; }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Course GetByID(long _id)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllCourse,"Course.ID="+_id)).First();
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Course> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllCourse, " Course.IsDeleted = 'false' and 1=1")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Course> GetListByLINQ(Func<CoreData.Course, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Course.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Course GetOneByLINQ(Func<CoreData.Course, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Course.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Course> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllCourse, "Course.IsDeleted = 'false' and " + _searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Course)_model, Table.Course.ToString());
               return true;
           }
           catch { return false; }
       }



       /// <summary>
       /// Delete a object by hidden
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool UpdateIsDelete(object _model)
       {
           try
           {
               var obj = (CoreData.Course)_model;
               obj.IsDeleted = true;
               entity.Update(obj, Table.Course.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method

      

      public CoreData.Course GetByCode(string Code)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllCourse, "Course.Code='" + Code+"'")).First();
           }
           catch { return null; }
       }

        public Course GetByName(String Name)
      {
          try
          {
              return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllCourse, "Course.Name='" + Name + "'")).First();
          }
          catch { return null; }
        }

       public Course GetbyClassID(long classid)
       {
           return entity.GetByCusTomSQL(String.Format(SQLCommand.GetCourseByClassID," Class.ID="+ classid)).ToList()[0];
       }

       bool Validate(Course obj, ref string mess)
       {

           new ValidateRequired("Mã khóa học", "Code", obj.Code);
           new ValidateRequired("Tên khóa học", "Name", obj.Name);
         //  new ValidateExist("Mã khóa học", "Code", "Code", obj, GetList().Cast<object>().ToList());
         //  new ValidateExist("Tên khóa học", "Name", "Name", obj, GetList().Cast<object>().ToList());
          
           mess = BusinessObject.Validate();

           BusinessObject.Clear();

           if (mess == null)
           {

               var code = GetByCode(obj.Code);
               if (code != null && (obj.ID != code.ID))
               {
                   mess = "Mã khóa bị trùng";
               }
           }

           if (mess == null)
           {

               var code = GetByName(obj.Name);
               if (code != null && (obj.ID != code.ID))
               {
                   mess = "Tên khóa bị trùng";
               }
           }
           return mess == null ? true : false;
       }



       public string GenCodeCourse()
       {
           try
           {
             

               string Autonum = "01";
               try
               {
                   var data = entity.GetByCusTomSQL(String.Format(courdecode)).First();

                  
                       var tempcode = data.Code.Substring(1);
                       int autonum = int.Parse(tempcode) + 1;
                       if (autonum <= 9)
                       {
                           Autonum = "0" + autonum.ToString();
                       }
                       else
                       {
                           Autonum = autonum.ToString();
                       }
                   
               }
               catch { }



               return Autonum ;

           }
           catch { return "01"; }
       }

        



        #endregion 
    
    }
         
}
